package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.entity.InformationEntity;
import com.ym.medical.domain.param.InformationParam;
import com.ym.medical.service.IInformationSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息管理
 */
@Api(tags = "消息管理")
@Controller
@RequestMapping("/management/information")
public class InformationManagement extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(InformationManagement.class);

	@Autowired
	private IInformationSV informationSV;

	@ApiOperation("消息列表")
	@GetMapping("/list")
	@ResponseBody
	public APIResponse<PageInfo<InformationEntity>> getInformationList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "informationModel", value = "消息信息", required = false)
					InformationParam informationModel) throws Exception {
		PageHelper.startPage(pageNo,limit);
		List<InformationEntity> informationPageInfo = informationSV
				.selectList(informationModel.covert());
		return successPage(informationPageInfo);
	}

	@Log
	@ApiOperation("消息新增")
	@PostMapping("/add")
	@ResponseBody
	public APIResponse insertInformation(
			@ApiParam(name = "information", value = "消息实体", required = true)
			@RequestBody @Validated(value = {InformationParam.Add.class}) InformationParam informationModel) {
		long flag = informationSV.insert(informationModel.covert());
		if (flag <= 0) {
			return failMsg("消息新增失败");
		}
		return successMsg("消息新增成功");
	}

	@Log
	@ApiOperation("消息修改")
	@PostMapping("/update")
	@ResponseBody
	public APIResponse updateInformation(
			@ApiParam(name = "information", value = "消息实体", required = true)
			@RequestBody @Validated(value = {InformationParam.Update.class}) InformationParam informationModel) {
		int flag = informationSV.updateById(informationModel.covert());
		if (flag == 0) {
			return failMsg("消息修改失败");
		}
		return successMsg("消息修改成功");
	}

	@Log
	@ApiOperation("消息删除")
	@PostMapping("/delete")
	@ResponseBody
	public APIResponse deleteInformation(
			@ApiParam(name = "ids", value = "消息主键", required = true) Long[] ids) {
		int rows = informationSV.deleteByIds(ids);
		if (rows <= 0) {
			return failMsg("消息删除失败");
		}
		return APIResponse.success("消息删除成功! rows:", rows);
	}

}